// matchUsers云函数代码
const cloud = require('wx-server-sdk');
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV });

const db = cloud.database();

exports.main = async (event, context) => {
  const { hobby } = event;

  try {
    // 查询数据库，匹配爱好为指定值的用户
    const result = await db.collection('userInfo')
      .where({
        hobbies: hobby
      })
      .get();

    return {
      code: 0,
      data: result.data
    };
  } catch (err) {
    console.error(err);
    return {
      code: -1,
      errMsg: '匹配用户失败'
    };
  }
};
